package ui;

import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.layers.AnnotationLayer;
import gov.nasa.worldwind.render.AnnotationAttributes;
import gov.nasa.worldwind.render.GlobeAnnotation;
import gov.nasa.worldwind.render.Material;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

import cas.CAS;
import cas.Identity;
import cas.Message;

public class ObjectDisplayer implements Runnable{
	private MainFrame mainFrame;
	private AnnotationAttributes geoAttr;
	private List<GlobeAnnotation> listAnnotations;
	private boolean isStarting;
	private boolean update;
	
	
	public ObjectDisplayer(MainFrame mainFrame) {
		super();
		this.mainFrame = mainFrame;
		listAnnotations = new ArrayList<GlobeAnnotation>();
	}
	@Override
	public void run() {
		setupAttribute();
		List<SurfaceObject> listObject = mainFrame.getListObject();
		CAS cas = mainFrame.getCas();
		WorldWindowGLCanvas wwd = mainFrame.getWwd();
		
		listObject.add(new SurfaceObject(cas.getMyIdentity().getKeretaApi().getIdKA(), 
				cas.getMyIdentity().getCurCoord().getLatitude().getDegrees(), 
				cas.getMyIdentity().getCurCoord().getLongitude().getDegrees(), Material.BLUE));
		listAnnotations.add(new GlobeAnnotation(cas.getMyIdentity().getKeretaApi().getIdKA()+"\n"+Float.valueOf(String.valueOf(cas.getMyIdentity().getSpeed(true)*3600.0/1000.0))+"Km/h", 
				cas.getMyIdentity().getPosition(),geoAttr));
		mainFrame.getAnnotLayer().addAnnotation(listAnnotations.get(0));
		wwd.getModel().getLayers().add(listObject.get(0));
		wwd.getModel().getLayers().add(mainFrame.getAnnotLayer());
		update = true;
		while(isStarting){
			cas.start();
			updateFrame();
			if(cas.getMyIdentity() != null&&isStarting){
				if(!listObject.get(0).marker.getPosition().equals(cas.getMyIdentity().getPosition())){
					listObject.get(0).marker.moveTo(cas.getMyIdentity().getPosition());
					listAnnotations.get(0).setText(cas.getMyIdentity().getKeretaApi().getIdKA()+"\n"+Float.valueOf(String.valueOf(cas.getMyIdentity().getSpeed(true)*3600.0/1000.0))+"Km/h");
					listAnnotations.get(0).moveTo(cas.getMyIdentity().getPosition());
					update = true;
				}
			}
			for(int i = 0;isStarting&& i<cas.getAnotherTrains().size();i++){
				Identity anotherTrain = cas.getAnotherTrains().get(i);
				if(!listObject.get(i+1).marker.getPosition().equals(anotherTrain.getPosition())){
					listObject.get(i+1).marker.moveTo(anotherTrain.getPosition());
					listAnnotations.get(i+1).setText(anotherTrain.getKeretaApi().getIdKA()+"\n"+Float.valueOf(String.valueOf(anotherTrain.getSpeed(true)*3600.0/1000.0))+"Km/h");
					listAnnotations.get(i+1).moveTo(anotherTrain.getPosition());
					update = true;
				}
			}
			if(update){
				wwd.redraw();
				update = false;
			}
			if(cas.isCollide()){
				System.out.println("Collide");
				isStarting = false;
			}
		}
		listObject.clear();
	}
	private void updateFrame(){
		CAS cas = mainFrame.getCas();
		if(cas.getMessage()==null){
			return;
		}
		String icon = "";
		switch (cas.getMessage().getStatus()) {
		case Message.DANGER:
			icon = "/icon/red.png";
			break;
		case Message.WARNING:
			icon = "/icon/yellow.png";
			break;
		case Message.SAFE:
			icon = "/icon/green.png";
			break;
		default:
			icon = "";
			break;
		}
		EastPanel eastPanel = mainFrame.getEastPanel();
		if(cas.isInSameDirection()){
			if(cas.isAtFront()){
				eastPanel.getRearLamp().setIcon(getImageIcon(icon));
			}else{
				eastPanel.getFrontLamp().setIcon(getImageIcon(icon));
			}
		}else{
			eastPanel.getFrontLamp().setIcon(getImageIcon(icon));
		}
		mainFrame.getSouthPanel().getLblSuggest().setText(cas.getMessage().getSuggest());
		mainFrame.getSouthPanel().lblTimemsg.setText("time left to stop : "+Float.valueOf(String.valueOf(cas.timeToStop))+"\n\n"+
				"time toward a safe distance : "+Float.valueOf(String.valueOf(cas.timeToSafeDistance)));
	}
	private void setupAttribute(){
		// Create default attributes
        AnnotationAttributes defaultAttributes = new AnnotationAttributes();
        defaultAttributes.setCornerRadius(10);
        defaultAttributes.setInsets(new Insets(8, 8, 8, 8));
        defaultAttributes.setBackgroundColor(new Color(0f, 0f, 0f, .5f));
        defaultAttributes.setTextColor(Color.WHITE);
        defaultAttributes.setDrawOffset(new Point(25, 25));
        defaultAttributes.setDistanceMinScale(.5);
        defaultAttributes.setDistanceMaxScale(2);
        defaultAttributes.setDistanceMinOpacity(.5);
        defaultAttributes.setLeaderGapWidth(14);
        defaultAttributes.setDrawOffset(new Point(20, 40));
        
		geoAttr = new AnnotationAttributes();
        geoAttr.setDefaults(defaultAttributes);
        geoAttr.setFrameShape(AVKey.SHAPE_NONE); 
        geoAttr.setFont(Font.decode("Arial-BOLD-12"));
        geoAttr.setTextColor(Color.CYAN);
        geoAttr.setTextAlign(AVKey.CENTER);
        geoAttr.setDrawOffset(new Point(0, 5)); 
        geoAttr.setBackgroundColor(Color.BLACK);
	}
	
	public boolean isStarting() {
		return isStarting;
	}
	public void setStarting(boolean isStarting) {
		this.isStarting = isStarting;
	}
	public boolean isUpdate() {
		return update;
	}
	public void setUpdate(boolean update) {
		this.update = update;
	}
	public AnnotationAttributes getGeoAttr() {
		return geoAttr;
	}
	public void setGeoAttr(AnnotationAttributes geoAttr) {
		this.geoAttr = geoAttr;
	}
	public List<GlobeAnnotation> getListAnnotations() {
		return listAnnotations;
	}
	public void setListAnnotations(List<GlobeAnnotation> listAnnotations) {
		this.listAnnotations = listAnnotations;
	}
	private ImageIcon getImageIcon(String name){
		return new ImageIcon(SouthPanel.class.getResource(name));
	}
}
